package cn.bellychang.loopqueue.arraylist.cl01noloop;

import cn.bellychang.iiterface.liuxin.Iterator;
import cn.bellychang.iiterface.liuxin.List;

/**
 * @author: bellychang
 * @date: 2020/2/8
 */
public class ArrayList<E> implements List<E> {

    public static final int REDUCE_CAPACITY_FACTOR = 4;
    private E[] array;

    private int head;

    public ArrayList(int capacity){
        if (capacity <= 1) {
            capacity = 2;
        }
        this.array = (E[]) new Object[capacity];
    }

    public ArrayList() {
        new ArrayList(2);
    }

    private int size;
    
    @Override
    public int getSize() {
        return this.size;
    }
    
    public int getCapacity() {
        return this.array.length;
    }
    
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is out of bound");
        }
        checkRange();
        // tail do not need to copy
        if (index != size) {
            for (int i = index; i < size; i++) {
                array[i + 1] = array[i];
            }
        }
        array[index] = e;

        size++;
    }

    @Override
    public E get(int index) {
        checkRemainingIndex(index);
        return array[index];
    }

    public E getFront() {
        return get(head);
    }

    private void checkRemainingIndex(int index) {
        if (index < head || index >= head + size) {
            throw new IllegalArgumentException("index out of bound");
        }
    }

    @Override
    public E remove(int index) {
        checkRemainingIndex(index);
        E temp = array[index];
        head++;
        size--;
        shrinkCapacity();
        return temp;
    }

    private void shrinkCapacity() {
        // if tail, do not need to copy array
        if (size<(array.length/4) && size>=2) {
            E[] target = (E[]) new Object[array.length / 2];
            System.err.printf("shrink capacity from [%d]-[%d] to [%d]-[%d]\n", head, (head + size - 1), 0, (size - 1));
            for (int i = 0; i < (size-head); i++) {
                target[i] = array[head+i];
            }
            array = target;
            head = 0;
        }
    }

    public void addFirst(E e) {
        add(head, e);
    }
    
    @Override
    public void addLast(E e) {
        add(size, e);
    }

    public E removeFirst() {
        return remove(head);
    }
    
    public E removeLast() {
        return remove(size - 1);
    }

    private void checkRange() {
        if (this.size >= array.length) {
            expandCapacity(this.size+(this.size>>1));
        }
    }

    private void expandCapacity(int capacity) {
        E[] target = (E[]) new Object[capacity];
        System.err.printf("expand capacity from [%d]-[%d] to [%d]-[%d]\n", 0, (size - 1), 0, (capacity - 1));
        for (int i = 0; i < size; i++) {
            target[i] = array[i];
        }
        array = target;
    }

    public Iterator<E> iterator() {
        return new ArrayListIterator<>();
    }

    private class ArrayListIterator<E> implements Iterator<E>{

        private int position;

        @Override
        public boolean hasNext() {
            return position<size;
        }

        @Override
        public E next() {
            return (E) array[position++];
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        int current = head;
        for (int i = head; i < current + size; i++) {
            builder.append(array[i])
                    .append("->");
        }
        return builder.length() >= 2 ? builder.toString().substring(0, builder.length() - 2) : builder.toString();
    }
}
